//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package org_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/org_report_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BasicDayReportInfoDbItem struct {
	DayTime int64  `bson:"dayTime"`
	Content string `bson:"content"`
}

type DayReportInfoDbItem struct {
	ReportId     string                   `bson:"_id"`
	OrgId        string                   `bson:"orgId"`
	BasicInfo    BasicDayReportInfoDbItem `bson:"basicInfo"`
	MemberUserId string                   `bson:"memberUserId"`
	CreateTime   int64                    `bson:"createTime"`
	UpdateTime   int64                    `bson:"updateTime"`
}

func (item *DayReportInfoDbItem) ToDayReportInfo() *org_report_api.DayReportInfo {
	return &org_report_api.DayReportInfo{
		ReportId: item.ReportId,
		BasicInfo: &org_report_api.BasicDayReportInfo{
			DayTime: item.BasicInfo.DayTime,
			Content: item.BasicInfo.Content,
		},
		MemberUserId: item.MemberUserId,
		CreateTime:   item.CreateTime,
		UpdateTime:   item.UpdateTime,
		UserPerm:     &org_report_api.UserPerm{},
	}
}

type _DayReportDao struct {
	mc *mongo.Collection
}

func newDayReportDao(db *mongo.Database) (*_DayReportDao, error) {
	mc := db.Collection(db_ns.DB_NS_ORG + "day_report")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"orgId", 1}, {"memberUserId", 1}, {"basicInfo.dayTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_DayReportDao{mc: mc}, nil
}

func (db *_DayReportDao) Insert(ctx context.Context, dbItem *DayReportInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_DayReportDao) Exist(ctx context.Context, orgId, memberUserId, reportId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": reportId, "orgId": orgId, "memberUserId": memberUserId})
	return count > 0, err
}

func (db *_DayReportDao) Get(ctx context.Context, orgId, memberUserId, reportId string) (*DayReportInfoDbItem, error) {
	retItem := &DayReportInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": reportId, "orgId": orgId, "memberUserId": memberUserId}).Decode(retItem)
	return retItem, err
}

func (db *_DayReportDao) Remove(ctx context.Context, orgId, memberUserId, reportId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": reportId, "orgId": orgId, "memberUserId": memberUserId})
	return err
}

func (db *_DayReportDao) RemoveByOrg(ctx context.Context, orgId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"orgId": orgId})
	return err
}

func (db *_DayReportDao) Count(ctx context.Context, orgId, memberUserId string, listParam *org_report_api.ListDayReportParam) (uint32, error) {
	query := bson.M{"orgId": orgId, "memberUserId": memberUserId}
	if listParam.FilterByDayTime {
		query["basicInfo.dayTime"] = bson.M{"$gte": listParam.FromDayTime, "$lte": listParam.ToDayTime}
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_DayReportDao) List(ctx context.Context, orgId, memberUserId string, listParam *org_report_api.ListDayReportParam, offset, limit int64) ([]*DayReportInfoDbItem, error) {
	query := bson.M{"orgId": orgId, "memberUserId": memberUserId}
	if listParam.FilterByDayTime {
		query["basicInfo.dayTime"] = bson.M{"$gte": listParam.FromDayTime, "$lte": listParam.ToDayTime}
	}
	cursor, err := db.mc.Find(ctx, query, options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"basicInfo.dayTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*DayReportInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_DayReportDao) Update(ctx context.Context, orgId, memberUserId, reportId string, dbItem *BasicDayReportInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": reportId, "orgId": orgId, "memberUserId": memberUserId},
		bson.M{"$set": bson.M{"basicInfo": dbItem, "updateTime": updateTime}})
	return err
}
